
const express = require('express');
const router = express.Router();
const pool = require('../mysql');

// 获取所有票种
router.get('/tickets', (req, res, next) => {
    pool.query('select * from tickets', [], (err, r) => {
        if (err) {
            next(err);
            return
        }
        res.send({ code: 200, data: r })
    })
})

var extend = function (o, n) {
    for (var p in n) {
        o[p] = n[p];
    }
    return o
}

// 获取用户拥有票卡
router.get('/user_ticket', (req, res, next) => {
    let mobile = req.query.mobile;
    pool.query('select * from cards where `mobile` = ?', [mobile], (err, r) => {
        if (err) {
            next(err);
            return
        }
        res.send({ code: 200, data: r })
    })
})

// 获取用户信息
router.get('/user_info', (req, res, next) => {
    let mobile = req.query.mobile;
    pool.query('select mobile,createdAt,integral,growth from users where `mobile` = ?', [mobile], (err, r) => {
        if (err) {
            next(err);
            return
        }
        if (r.length > 0) {
            res.send({ code: 200, data: r[0] })
            return
        }
        res.send({ code: 200, data: r })
    })
})

// 获取所有票种
router.post('/buy_ticket', (req, res, next) => {
    console.log("-==========================")
    queryResponse = ""
    req.on('data', function (chunk) {
        console.log(queryResponse)
        queryResponse += chunk;
    });

    req.on('end', function () {
        console.log(queryResponse)
        try {
            var body = JSON.parse(queryResponse)
            let tickets = body.tickets
            let mobile = body.mobile;
            var haveError = false
            var flag = 0;
            for (let i = 0; i < tickets.length; i++) {
                let desc = tickets[i].desc;
                let count = tickets[i].count;
                var e = pool.query('select stock from tickets where `desc` = ?', [desc], (err, r) => {
                    flag++
                    if (err) {
                        next(err);
                        return
                        haveError = true
                    }
                    if (r.length == 0 || r[0].stock < count) {
                        haveError = true
                    }
                    if (flag == tickets.length) {
                        if (haveError) {
                            res.send({ code: 500, msg: desc + "库存不足" })
                            return
                        }
                        for (let i = 0; i < tickets.length; i++) {
                            let desc = tickets[i].desc;
                            let count = tickets[i].count;
                            pool.query('update  tickets set stock=stock-? where `desc` = ?', [count, desc], (err, r) => {
                                if (err) {
                                    next(err);
                                    return
                                }
                            })

                            pool.query('select id from cards where `mobile` = ? and `desc` = ?', [mobile, desc], (err, r) => {
                                if (err) {
                                    next(err);
                                    return
                                }
                                if (r.length > 0) {
                                    pool.query('update cards set `count`=`count`+? where `mobile`=? and `desc`=?', [count, mobile, desc], (err, r) => {
                                        if (err) {
                                            next(err);
                                            return
                                        }
                                    })
                                    return
                                }

                                pool.query('insert into cards(`mobile`,`desc`,`count`) values (?,?,?)', [mobile, desc, count], (err, r) => {
                                    if (err) {
                                        next(err);
                                        return
                                    }
                                })
                            })
                        }

                    }
                })
            }
        } catch {
            next("非法的JSON格式")
        }
    });
})

// 获取所有票种
router.post('/buy_ticket1', (req, res, next) => {
    let mobile = req.body.mobile;
    let desc = req.body.desc;
    let count = req.body.count;
    console.log(mobile,count,desc)
    var e = pool.query('select stock from tickets where `desc` = ?', [desc], (err, r) => {
        if (err) {
            next(err);
            return
        }
        if (r.length == 0 || r[0].stock < count) {
            res.send({ code: 500, msg: desc + "库存不足" })
            return
        }
        pool.query('update  tickets set stock=stock-? where `desc` = ?', [count, desc], (err, r) => {
            if (err) {
                next(err);
                return
            }
        })
        pool.query('select id from cards where `mobile` = ? and `desc` = ?', [mobile, desc], (err, r) => {
            if (err) {
                next(err);
                return
            }
            if (r.length > 0) {
                pool.query('update cards set `count`=`count`+? where `mobile`=? and `desc`=?', [count, mobile, desc], (err, r) => {
                    if (err) {
                        next(err);
                        return
                    }
                })
            } else {
            pool.query('insert into cards(`mobile`,`desc`,`count`) values (?,?,?)', [mobile, desc, count], (err, r) => {
                if (err) {
                    next(err);
                    return
                }
            })
        }

        res.send({code:200,msg:"购票成功"})
        })
    })
})

// 导出路由模块
module.exports = router;